﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Controls
{
	using Service;

	public partial class ShoppingCart : BaseUserControl
	{
		private IShoppingCartItemService _cartService;
		private IProductService _productService;
		private IShoppingCartItemService _ShoppingCartItemService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/Login.aspx");
			}
		}

		protected void ButtonCheckout_Click(object sender, EventArgs e)
		{
			if(SiteContext.Current.User != null)
			{
				Server.Transfer("~/Ordering.aspx");
			}
			else
			{
				Server.Transfer("~/Login.aspx");
			}
		}

		protected void ButtonUpdateCart_Click(object sender, EventArgs e)
		{
			if(_ShoppingCartItemService == null)
			{
				_ShoppingCartItemService = new ShoppingCartItemService();
			}

			foreach(GridViewRow cartItem in GridCartItems.Rows)
			{
				var shoppingCartItemID = (int)GridCartItems.DataKeys[cartItem.RowIndex]["ShoppingCartItemID"];
				var theShoppingCartItem = _ShoppingCartItemService.GetTheShoppingCartItem(shoppingCartItemID);

				if(theShoppingCartItem != null)
				{
					var TextQuantity = cartItem.FindControl("TextQuantity") as TextBox;
					int quantity = 0;
					if(int.TryParse(TextQuantity.Text.Trim(), out quantity))
					{
						theShoppingCartItem.Quantity = quantity;

						_ShoppingCartItemService.UpdateShoppingCartItem(theShoppingCartItem);
					}
				}
			}
		}

		protected void GridCartItems_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if(e.CommandName == "RemoveCartItem")
			{
				if(_cartService == null)
				{
					_cartService = new ShoppingCartItemService();
				}

				_cartService.DeleteShoppingCartItem(int.Parse(e.CommandArgument.ToString()));
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			this.LShoppingCartHeader.Text = GetLocalResourceString("ShoppingCart.Header");
			this.LPriceSummaryLabel.Text = GetLocalResourceString("ShoppingCart.PriceSummaryLable");
			this.LCurrency.Text = GetLocalResourceString("ECommerce.Currency");
			ButtonUpdateCart.Text = GetLocalResourceString("ShoppingCart.UpdateCartButtonText");
			ButtonCheckout.Text = GetLocalResourceString("ShoppingCart.CheckoutButtonText");

			BindCartitems();
		}

		protected void GridCartItems_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("ShoppingCart.ProdNameCellHeaderText");
				e.Row.Cells[2].Text = GetLocalResourceString("ShoppingCart.QuantityCellHeaderText");
				e.Row.Cells[3].Text = GetLocalResourceString("ShoppingCart.ListPriceCellHeaderText");
			}

			if(e.Row.DataItem != null)
			{
				HyperLink hlImg = (HyperLink)e.Row.FindControl("HyperLinkProductImg");

				hlImg.ImageUrl = "~/PictureHandler.ashx?picturesize=90&productid=" + GridCartItems.DataKeys[e.Row.RowIndex].Value.ToString();
				hlImg.NavigateUrl = "~/ProductDetail.aspx?productid=" + GridCartItems.DataKeys[e.Row.RowIndex].Value.ToString();

				HyperLink hlProd = (HyperLink)e.Row.FindControl("HyperLinkProduct");
				hlProd.NavigateUrl = "~/ProductDetail.aspx?productid=" + GridCartItems.DataKeys[e.Row.RowIndex].Value.ToString();

				LinkButton lbtn = (LinkButton)e.Row.FindControl("LinkButtonRemove");
				lbtn.Text = GetLocalResourceString("ShoppingCart.RemoveCartItemLinkText");
			}
		}

		private void BindCartitems()
		{
			var user = SiteContext.Current.User;

			if (user != null && SiteContext.Current.WorkingLanguage != null)
			{
				if (_cartService == null)
				{
					_cartService = new ShoppingCartItemService();
				}

				if (_productService == null)
				{
					_productService = new ProductService();
				}

				Guid userID = new Guid(user.ProviderUserKey.ToString());
				int currentLanguageID = SiteContext.Current.WorkingLanguage.LanguageId;

				var cartItems = _cartService.GetShoppingCartItemByUser(userID);

				if (cartItems != null)
				{
					var cartList = (from cart in cartItems
									join prod in _productService.GetProductsByLanguage(currentLanguageID) on cart.ProductID equals prod.ProductID
									select new
									{
										ProductID = prod.ProductID,
										ProductName = prod.Name,
										ListPrice = prod.ListPrice,
										Quantity = cart.Quantity,
										ShoppingCartItemID = cart.ShoppingCartItemID
									});

					GridCartItems.DataSource = cartList;
					GridCartItems.DataBind();

					ButtonCheckout.Enabled = (GridCartItems.Rows.Count > 0);
					ButtonUpdateCart.Enabled = (GridCartItems.Rows.Count > 0);
				}
				else
				{
					GridCartItems.DataSource = null;
					GridCartItems.DataBind();

					ButtonCheckout.Enabled = false;
					ButtonUpdateCart.Enabled = false;
				}
			}
			else
			{
				Server.Transfer("~/Login.aspx");
			}
		}
	}
}